<?php

include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/frontend/controller/menucontrollers/MenuController.php';
include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/frontend/controller/eventfinders/PersonSectorInvolvementEventFinder.php';
include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/frontend/controller/commands/PersonSectorInvolvementCommandFactory.php';

/**
 * Description of PersonSectorInvolvementMenuController
 *
 * @author Reinowin7
 */
class PersonSectorInvolvementMenuController implements MenuController
{
    const SUCCESSFUL_OPERATION_RESULT_INDICATOR = "1";
    const UNSUCCESSFUL_OPERATION_RESULT_INDICATOR = "0";
    
    public function service($request) 
    {
        $userView = "insertPersonSectorInvolvementPage";
        $eventFinder = new PersonSectorInvolvementEventFinder();
        $personSectorInvolvementCommandFactory = new PersonSectorInvolvementCommandFactory();
        $command = null ;
        $event = null;
        $operationResult = self::SUCCESSFUL_OPERATION_RESULT_INDICATOR;

        try 
        {
            $event = $eventFinder->findEvent($request);
            $command = $personSectorInvolvementCommandFactory->getCommand($event);
            
            if (!is_null($command))
            {
                $operationResult = $command->executeCommand($request);
            }


            if ($operationResult == self::SUCCESSFUL_OPERATION_RESULT_INDICATOR
                    and ($event == "insertPersonSectorInvolvementPage"))
            {
                $userView = "insertPersonSectorInvolvementPage";
            }

            if ($operationResult == self::UNSUCCESSFUL_OPERATION_RESULT_INDICATOR
                    and ($event == "insertPersonSectorInvolvementPage"))
            {
                $userView = "insertPersonSectorInvolvementPage";
            }


            if ($operationResult == self::SUCCESSFUL_OPERATION_RESULT_INDICATOR
                    and ($event == "insertPersonSectorInvolvementFormPostEvent"))
            {
                echo 'Persisting data into a database was successful!';
                $userView = "insertPersonSectorInvolvementPage";
            }

            if ($operationResult == self::UNSUCCESSFUL_OPERATION_RESULT_INDICATOR
                    and ($event == "insertPersonSectorInvolvementFormPostEvent"))
            {
                $userView = "insertPersonSectorInvolvementPage";
            }
        }
        catch (Exception $ex) 
        {
            $ex->printStackTrace();
        }

        return $userView;
    }
}

?>
